﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace genQiang
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        Random random = new Random();
        string srcChars;
        string dstChars;
        bool isks;
        int sorce;


        private void init()
        {
            开始ToolStripMenuItem.Enabled = true;
            label2.Text =textBox1.Text= "";
            progressBar1.Value= progressBar1.Maximum;
            timer1.Interval = 30;
            textBox1.Enabled = button1.Enabled = timer1.Enabled = progressBar1.Visible= false;
            srcChars = "";
            dstChars = "";
            sorce = 1;
            this.MaximizeBox = false;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            label1.Text = "";
            locked = true;
        }
        private void Form4_Load(object sender, EventArgs e)
        {
            init();
        }

        //48-57     数字1
        //65-90     大写2
        //97-122    小写3
        private void GenSrcChars()
        {
            srcChars = "";
            int len = sorce / 2 + 3;

            for (int i = 0; i < len; i++)
            {
                int charopt = random.Next(1, 4);
                char newChar = '0';
                switch (charopt)
                {
                    case 1:
                        newChar=(char)random.Next(48, 58);
                        break;
                    case 2:
                        newChar = (char)random.Next(65, 91);
                        break;
                    case 3:
                        newChar = (char)random.Next(97, 123);
                        break;
                    default:
                        MessageBox.Show("错误", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        break;
                }
                srcChars+=(newChar);
            }
            label1.Text = srcChars;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dstChars = textBox1.Text;
            if (srcChars.Equals(dstChars))
            {
                button1.Enabled = textBox1.Enabled = false;
                sorce++;
                GenSrcChars();
                progressBar1.Value = progressBar1.Maximum;
                locked = true;
                textBox1.Text = "";
            }
            else
            {
                label1.Text = srcChars;
                timer1.Enabled = false;
                DialogResult dialogResult = MessageBox.Show("你一共进行了:" + (sorce-1).ToString() + "关 是否上传成绩?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    string httpresp = HttpUtils.HttpGet("cmt/" + MainWin.JYCS + "/" + (sorce - 1).ToString() + "/" + MainWin.youName);
                    MessageBox.Show(httpresp);
                }
                init();
            }

        }

        private void 开始ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            开始ToolStripMenuItem.Enabled= false;
            GenSrcChars();
            timer1.Enabled = progressBar1.Visible =true;
        }

        bool locked;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (progressBar1.Value > 0)
            {
                progressBar1.Value--;
            }
            else
            {
                if (locked)
                {
                    label1.Text = "*****";
                    button1.Enabled = textBox1.Enabled = true;
                    textBox1.Focus();
                    locked= false;
                }
            }
            label2.Text = "当前是第" + sorce.ToString() + "轮";
        }

        private void Form4_FormClosed(object sender, FormClosedEventArgs e)
        {
            MainWin.mainWin.Visible = true;
        }

        private void 说明ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("PS\r\n点击开始,生成随机字符,3秒內记住然后输入验证",
    "说明", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                this.button1_Click(sender, e);
            }
        }
    }
}
